/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.standardcloud.gateway.service;

import com.hyts.standardcloud.gateway.model.GatewayFilterDefinition;
import com.hyts.standardcloud.gateway.model.GatewayPredicateDefinition;
import com.hyts.standardcloud.gateway.model.GatewayRouteDefinition;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 * @project-name:standard-cloud
 * @package-name:com.hyts.standardcloud.gateway.service
 * @author:LiBo/Alex
 * @create-date:2020-01-05 22:26
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public interface DaoRouteService {

    /**
     * 添加路由对象
     * @param routeDefinition
     * @return
     */
    boolean addRoute(RouteDefinition routeDefinition);

    /**
     * 编辑路由对象
     * @param routeDefinition
     * @return
     */
    boolean editRoute(RouteDefinition routeDefinition);

    /**
     * 删除路由对象
     * @param routeDefinition
     * @return
     */
    boolean removeRoute(RouteDefinition routeDefinition);

    /**
     * 搜索路由对象
     * @param searchKey
     * @return
     */
    Mono<List<RouteDefinition>> searchRoute(String searchKey);


    //把前端传递的参数转换成路由对象
    default RouteDefinition assembleRouteDefinition(GatewayRouteDefinition gwdefinition) {
        RouteDefinition definition = new RouteDefinition();
        definition.setId(gwdefinition.getId());
        definition.setOrder(gwdefinition.getOrder());

        //设置断言
        List<PredicateDefinition> pdList=new ArrayList<>();
        List<GatewayPredicateDefinition> gatewayPredicateDefinitionList=gwdefinition.getPredicates();
        for (GatewayPredicateDefinition gpDefinition: gatewayPredicateDefinitionList) {
            PredicateDefinition predicate = new PredicateDefinition();
            predicate.setArgs(gpDefinition.getArgs());
            predicate.setName(gpDefinition.getName());
            pdList.add(predicate);
        }
        definition.setPredicates(pdList);

        //设置过滤器
        List<FilterDefinition> filters = new ArrayList();
        List<GatewayFilterDefinition> gatewayFilters = gwdefinition.getFilters();
        for(GatewayFilterDefinition filterDefinition : gatewayFilters){
            FilterDefinition filter = new FilterDefinition();
            filter.setName(filterDefinition.getName());
            filter.setArgs(filterDefinition.getArgs());
            filters.add(filter);
        }
        definition.setFilters(filters);
        URI uri = null;
        if(gwdefinition.getUri().startsWith("http")){
            uri = UriComponentsBuilder.fromHttpUrl(gwdefinition.getUri()).build().toUri();
        }else{
            uri = URI.create(gwdefinition.getUri());
        }
        definition.setUri(uri);
        return definition;
    }

}
